//
//  ViewController2.swift
//  UIKit物理效果
//
//  Created by 柯南 on 2022/9/15.
//

import UIKit
import WebKit
import MJRefresh

//MARK:- 惯性滑动Demo
class ViewController2: UIViewController {
    
    var gestureControl: YTWebScrollGestureControl?
    
    lazy var contentView = YTReferenceView()
    lazy var tableView = UITableView()
    lazy var webView = WKWebView()
    
    // ======================

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        self.navigationController?.navigationBar.isTranslucent = false
        self.title = "Web"
        
        if #available(iOS 15.0, *) {
            let bar = UINavigationBarAppearance()
            bar.backgroundColor = UIColor.white
            bar.titleTextAttributes = [NSAttributedString.Key.foregroundColor :UIColor.black]
            self.navigationController?.navigationBar.scrollEdgeAppearance = bar // 可滑动界面配置
            self.navigationController?.navigationBar.standardAppearance = bar // 普通页面配置
        }
        // ===============================

        self.view.addSubview(contentView)
        self.contentView.addSubview(tableView)
        
        // contentView
        contentView.frame = .init(x: 0, y: 64, width: view.bounds.width, height: view.bounds.height - 64)
        
        // tableView
        tableView.frame = .init(x: 0, y: 0, width: contentView.bounds.width, height: contentView.bounds.height)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = 100
        tableView.register(TableViewCustomCell.self, forCellReuseIdentifier: "cell")
        tableView.tableHeaderView = webView
        
        // BackNormalFoot 不能用，可以用 AutoNormalFotter 或 AutoGifFooter
        tableView.mj_footer = MJRefreshAutoGifFooter(refreshingBlock: {
            DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
                self.tableView.mj_footer?.endRefreshing()
                print("上拉加载")
            }
        })
        
        // webView
        webView.load(URLRequest(url: URL(string: "https://article.yutanglabs.com/?p=11086")!))
        webView.frame = .init(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height - 64)
        
        // gestureControl
        gestureControl = .init(referenceView: contentView, tableView: tableView, webView: webView)
        gestureControl?.delegate = self
    }
}

extension ViewController2: UITableViewDelegate, UITableViewDataSource, YTWebScrollGestureControlDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.selectionStyle = .none
        cell.textLabel?.text = "\(indexPath.row)"
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("\(indexPath)")
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
//        print(scrollView.contentOffset.y)
    }
    
    func webScrollGestureControlDidScroll(_ control: YTWebScrollGestureControl) {
        
    }
}

class TableViewCustomCell: UITableViewCell {
    lazy var btn = UIButton()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.addSubview(btn)
        btn.setTitle("按钮", for: .normal)
        btn.backgroundColor = .orange
        btn.frame = .init(x: 100, y: 25, width: 80, height: 50)
        btn.addTarget(self, action: #selector(click), for: .touchUpInside)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func click() {
        print("点击 Cell Btn")
    }
}
